import { Node, Prefab } from "cc";
import ResLoader, { AssetBundle, ErrorCallback } from "../../sgame/resource/ResLoader";
import SUI, { start_loading } from "../../sgame/ui/SUI";
import { SceneManager } from "./SceneManager";


export class GameScene {
    public name: string;
    public path: string;   // 场景资源路径
    public bundleNames: string[];    // 场景所依赖的资源包列表

    public bundles: AssetBundle[] = [];    // 加载后的 bundle
    private _prefab: Prefab;  // 场景预设
    private _node: Node;  // 场景实例节点
    private _loaded: boolean = false;

    // 定时删除的节点
    private _timerToDestroy: number = 0;
    private _autoUnloadTime: number = 0;

    private sceneManager: SceneManager;

    // 检查场景是否已加载
    public get loaded(): boolean {
        return this._loaded;
    }

    constructor(name: string, path: string, bundleNames: string[]) {
        this.name = name;
        this.path = path;
        this.bundleNames = bundleNames;
        this.sceneManager = SceneManager.instance();
    }

    load(onComplete?: Function, onError?: ErrorCallback) {
        if (this._loaded) {
            onComplete && onComplete();
            return;
        }

        start_loading(this.bundleNames, (bundles: AssetBundle[]) => {
            this.bundles = bundles;
            this._loaded = true;
            this._prefab = ResLoader.GetAsset(this.path, Prefab);
            // 加载完成，关闭定时器
            clearTimeout(this._timerToDestroy);
            onComplete && onComplete();
        }, null, onError);
    }

    public autoUnload(value: number) {
        this._autoUnloadTime = value;
        if (this._autoUnloadTime <= 0) {
            clearTimeout(this._timerToDestroy);
            this._timerToDestroy = 0;
        }
    }

    show(onComplete?: () => void, onError?: ErrorCallback) {
        if (this._node) {
            onComplete && onComplete();
        } else if (this.loaded) {
            this._node = SUI.InitPrefab(this._prefab, this.sceneManager.node);
            onComplete && onComplete();
        } else {
            this.load(() => {
                this._node = SUI.InitPrefab(this._prefab, this.sceneManager.node);
                onComplete && onComplete();
            }, onError);
        }
    }

    destroy() {
        if (!this._node) {
            return;
        }

        this._node.destroy();
        this._node = null;
        if (this._autoUnloadTime) {
            this._timerToDestroy = setTimeout(() => {
                this.unload();
            }, this._autoUnloadTime);
        }
    }

    destroyed() {
        return this._node == null;
    }

    unload() {
        this.destroy();

        this.bundles.forEach((bundle: AssetBundle) => {
            bundle.releaseAll();
        });

        this._loaded = false;
        // 已经卸载完成，关闭定时器
        clearTimeout(this._timerToDestroy);
    }

    hide() {
        if (this._node) {
            this._node.active = false;
        }
    }

    // 重新加载场景
    reload(onComplete?: Function, onError?: ErrorCallback) {
        this.load(onComplete, onError);
    }

    // 检查场景是否可见
    isVisible(): boolean {
        return this._node ? this._node.active : false;
    }
}
